package testdatagenerator;

import java.util.Random;
import java.util.Vector;
import javax.swing.JOptionPane;


public class Attack {
	Random gen;
	private int size;
	private boolean randConnectedNodes;
	private Vector<Integer> AttackerIds;
	private double mean, stdDev, OriginFreq, dropRate;
	public Attack(){
		size = 0;
		gen = new Random();
		AttackerIds = new Vector<Integer>();
		mean = -1;
		stdDev = -1;
		randConnectedNodes = false;
	}
	//Get whether or not the AttackerIds should be randomly picked
	public boolean getRandConnectedNodes(){
		return randConnectedNodes;
	}
	//set whether or not the AttackerIds should be randomly picked
	public void setRandConnectedNodes(boolean a){
		randConnectedNodes = a;
	}
	
	//if the colluderIds should be randomly picked, the size of the ColluderIds vector 
	//doesn't matter, for in that case the nodes haven't been picked yet. In this case,
	//size indicates what the size will be, and thus the user should be able to set it.
	public void setSize(int a){
		if (randConnectedNodes){
			size = a;
		}
	}
	
	//if the user opted to have this attack be comprised of  randomly connected nodes
	//then decide on which nodes will be a part of this attack.
	public void pickMembers(){
		if (randConnectedNodes){
			int loop = size;
			Vector<Node> neighbors = new Vector<Node>(), nodes = new Vector<Node>();
			Node toBeAdded = Node.getHighlyConnectedNode();
			nodes.add(toBeAdded);
			while(loop > 0){
				AttackerIds.add(toBeAdded.getId());
				for(int j=0;j<toBeAdded.getNumNeigh();j++){
					GeneralMethods.addToVec(neighbors, nodes, toBeAdded.getNeigh(j));
					GeneralMethods.addToVec(nodes, nodes,  toBeAdded.getNeigh(j));
				}
				toBeAdded = neighbors.remove(gen.nextInt(neighbors.size()));
				loop--;
			}
		}
	}
	
	public void displayMembers(String ColludeName){
		if (randConnectedNodes){
			String display = "The nodes of collusion "+ColludeName+" are:";
			for(int i=0;i<size;i++){
				display += " Node"+((Integer)AttackerIds.get(i)).toString();
			}
			display += ".";
			JOptionPane.showMessageDialog(null, display, "Pay Attention!", JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	
	
	public int getSize() {
		return size;
	}
	
	//set the weight of how likely this attack is to generate packets
	public void setFreq(double Originfreq){
		OriginFreq = Originfreq;
	}
	public double getFreq(){
		return OriginFreq;
	}
	public double getMean(){
		return mean;
	}
	//return whether or not the node with id=id is a part of this attack
	public boolean contains(int id){
		return AttackerIds.contains(id);
	}
	public double getStdDev(){
		return stdDev;
	}
	
	public double getDropRate(){
		return dropRate;
	}
	
	public void setMean(double Mean){
		mean = Mean;
	}
	public void setStdDev(double StdDev){
		stdDev = StdDev;
	}
	public void addAttack(int id){
		if (!AttackerIds.contains(id)){
			AttackerIds.add(id);
			size++;
		}	
	}
	public void clearAttacks(){
		AttackerIds.clear();
	}
	@SuppressWarnings("unchecked")
	public Vector<Integer> getMemberIds(){
		return ((Vector<Integer>) AttackerIds.clone());
	}
	public Vector<Node> getMembers(){
		Vector<Node> ret = new Vector<Node>();
		for(int i=0;i<AttackerIds.size();i++){
			ret.add(Node.getNodeById(AttackerIds.get(i)));
		}
		return ret;
	}
	public Node getRandomMember(){
		int id = getRandomMemberId();
		return Node.getNodeById(id);
	}
	public int getRandomMemberId(){
		if (AttackerIds.size()< 1){
			return 0;
		}
		int ndx = gen.nextInt(AttackerIds.size());
		return AttackerIds.get(ndx);
	}
}
